// 根据时间模板、天数,返回时间字符串
export function nowDateBytemplate(temStr, addDay) {
    addDay = addDay || 0;
    let date = new Date(new Date().getTime() + addDay * 24 * 60 * 60 * 1000),
        year = date.getFullYear(),
        month = String(date.getMonth() + 1).padStart(2, '0'),
        day = String(date.getDate()).padStart(2, '0');
    // hour = date.getHours(),
    // minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
    // second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    if (temStr === 'yyyy') {
        return year;
    } else if (temStr === 'yyyyMM') {
        return `${year}${month}`;
    } else if (temStr === 'yyyyMMdd') {
        return `${year}${month}${day}`;
    } else if (temStr === 'yyyy-MM-dd') {
        return `${year}-${month}-${day}`;
    }
}
// 本周时间计算
export function weekNow(temStr, addDa) {
    let arr = [],
        result = [],
        nowDay = nowDateBytemplate(temStr, addDa),

        weekDay = ['0', '1', '2', '3', '4', '5', '6'];
    nowDay.split('-').forEach(item => {
        arr.push(item * 1);
    });

    let myDate = new Date(Date.parse(arr.join('/'))).getDay();
    if (weekDay[myDate] === '1') {
        return nowDateBytemplate('yyyyMMdd');
    }
    if (weekDay[myDate] === '2') {
        result = [nowDateBytemplate('yyyyMMdd', -1), nowDateBytemplate('yyyyMMdd')];
        return result.join(',');
    }
    if (weekDay[myDate] === '3') {
        result = [nowDateBytemplate('yyyyMMdd', -2), nowDateBytemplate('yyyyMMdd', -1), nowDateBytemplate('yyyyMMdd')];
        return result.join(',');
    }
    if (weekDay[myDate] === '4') {
        result = [nowDateBytemplate('yyyyMMdd', -3), nowDateBytemplate('yyyyMMdd', -2), nowDateBytemplate('yyyyMMdd', -1), nowDateBytemplate('yyyyMMdd')];
        return result.join(',');
    }
    if (weekDay[myDate] === '5') {
        result = [
            nowDateBytemplate('yyyyMMdd', -4),
            nowDateBytemplate('yyyyMMdd', -3),
            nowDateBytemplate('yyyyMMdd', -2),
            nowDateBytemplate('yyyyMMdd', -1),
            nowDateBytemplate('yyyyMMdd')
        ];

        return result.join(',');
    }
    if (weekDay[myDate] === '6') {
        result = [
            nowDateBytemplate('yyyyMMdd', -5),
            nowDateBytemplate('yyyyMMdd', -4),
            nowDateBytemplate('yyyyMMdd', -3),
            nowDateBytemplate('yyyyMMdd', -2),
            nowDateBytemplate('yyyyMMdd', -1),
            nowDateBytemplate('yyyyMMdd')
        ];
        return result.join(',');
    }
    if (weekDay[myDate] === '0') {
        result = [
            nowDateBytemplate('yyyyMMdd', -6),
            nowDateBytemplate('yyyyMMdd', -5),
            nowDateBytemplate('yyyyMMdd', -4),
            nowDateBytemplate('yyyyMMdd', -3),
            nowDateBytemplate('yyyyMMdd', -2),
            nowDateBytemplate('yyyyMMdd', -1),
            nowDateBytemplate('yyyyMMdd')
        ];
        return result.join(',');
    }
}
// 获取当前月
export function getNowMonth() {
    let date = new Date(),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate();
    month = String(month).padStart(2, '0');
    day = String(day).padStart(2, '0');
    let nowMonth = `${year}-${month}-${day}`;
    return nowMonth;
}
/**
 * @description 当前日期的上一个月
 * @param {number} isCurrentDate 是否有日
 * @return {string}  结果
 */
export function getPreMonth(isCurrentDate = false) {
    let arr = getNowMonth().split('-'),
        year = arr[0], // 获取当前日期的年份
        month = parseInt(arr[1]) - 1, // 获取当前日期的月份
        day = arr[2]; // 获取当前日期的月份
    if (month === 0) {
    // 1月的上一月是前一年的12月
        year = parseInt(year) - 1;
        month = 12;
    }
    month = String(month).padStart(2, '0');
    let preMonth = isCurrentDate ? `${year}-${month}-${day} ` : `${year}-${month}`;
    return preMonth;
}